package com.example.myapplication.utils;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myapplication.R;
import com.example.myapplication.data.User;
import com.google.firebase.crashlytics.buildtools.reloc.com.google.common.io.Resources;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

public class UserService {
    public static final MediaType JSON = MediaType
            .parse("application/json; charset=utf-8");
    private static final String TAG = "USERSERVICE";

    public static void login(User user, Handler h,int successFlag,int failFlag){
        OkHttpClient okHttpClient = new OkHttpClient();

        Request request = new Request.Builder().
                url("http://120.25.230.35:8080/login?userName="+user.getName()+"&passWd="+user.getPwd()).get().build();
        Call call = okHttpClient.newCall(request);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response execute = call.execute();
                    if(execute.isSuccessful()){

                        Message message = new Message();
                        //把jwt传进去
                        message.obj = execute.body().string();
                        message.what = successFlag;
                        h.sendMessage(message);
                    }else{
                        Message message = new Message();
                        message.what = failFlag;
                        h.sendMessage(message);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
